package com.fy.login.internal;

import com.fy.login.BaseMetadata;
import com.fy.login.IValidateChain;

import javax.security.auth.Subject;
import javax.security.auth.login.LoginException;
import java.util.ArrayList;
import java.util.List;
import java.util.ServiceLoader;
import java.util.stream.Collectors;

/**
 * @author zhufeifei 2022/8/7
 **/

public class ValidateChains extends ArrayList<IValidateChain> {
    public ValidateChains() {
        ServiceLoader<IValidateChain> serviceLoader = ServiceLoader.load(IValidateChain.class);
        if (serviceLoader.findFirst().isPresent()) {
            List<IValidateChain> list = serviceLoader.stream().map(ServiceLoader.Provider::get).collect(Collectors.toList());
            this.addAll(list);
        }
    }

    public void checkAll(BaseMetadata metadata, Subject subject) throws LoginException {
        for (IValidateChain ch : this) {
            ch.check(metadata, subject);
        }
    }
}
